package com.memorypalace.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.memorypalace.deck.biz.IDeckBiz;
import com.memorypalace.deck.vo.DeckNoteNoteTypeCardStyleCard;
import com.memorypalace.deck.vo.ScreenConditions;
import com.memorypalace.deck.vo.ScreenDate;
import com.memorypalace.pojo.CardStudyRecord;
import com.memorypalace.pojo.StudyInterval;
import com.memorypalace.pojo.StudyList;
import com.memorypalace.pojo.StudyScheme;
import com.memorypalace.pojo.User;
import com.memorypalace.study.biz.ICardStudyRecordBiz;
import com.memorypalace.study.biz.IStudyListBiz;
import com.memorypalace.util.AJaxUtil;

@Controller
@RequestMapping("/study")
public class StudyAction {
	@Autowired
	private ICardStudyRecordBiz csrBiz;
	@Autowired
	private IStudyListBiz islBiz;
	@Autowired
	private IDeckBiz deckBiz;
	

	// 学习记录
	@RequestMapping("/insertcsr")
	@ResponseBody
	public void insertCardStudyRecord(HttpServletRequest request,CardStudyRecord cardStudyRecord, HttpServletResponse response) {

		System.out.println(cardStudyRecord.getCardId());
		System.out.println(cardStudyRecord.getCostSeconds());
		System.out.println(cardStudyRecord.getAnswerButton());
		cardStudyRecord.setStudyTime(new Date());
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		cardStudyRecord.setUserId(userId);
		StudyInterval si = new StudyInterval();
		si.setDay(1);
		si.setMinutes(0);
		cardStudyRecord.setStudyInterval(si);
		csrBiz.insertCardStudyRecord(cardStudyRecord);
		AJaxUtil.sendJsonByObj(si, response);
	}

	// 获取学习列表
	@RequestMapping("/getsl")
	public String getStudyList(HttpServletRequest request,Map<String, Object> map) {

		System.out.println("-----------------------------------------------------------------");

		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();

		List<StudyList> listStudys = islBiz.selectStudyList(userId);// 模拟两个列表

		for (StudyList studyList : listStudys) {
			int oldCard = 0;
			int newCard = 0;
			StudyScheme studyScheme = islBiz.selectStudyListByStudyListId(studyList.getStudyListId()).getStudyScheme();
			studyList.setStudyScheme(studyScheme);
			ScreenConditions screenConditions = studyList.getScreenConditions();
//			ScreenDate c = new ScreenDate(null, "before", null, new Date());
//			screenConditions.setCardNextStudyTime(c);
			studyList.setScreenConditions(screenConditions);
			Integer newCardStudyCount = csrBiz.getTodayNewCardStudyCount(studyList);
			Integer reviewCardStudyCount = csrBiz.getTodayReviewCardStudyCount(studyList);
			// 根据已学习的个数,来修改学习列表对象中学习方案的限制
			Integer newCardCountTarget = studyList.getStudyScheme().getNewCardStudyScheme().getNewCardCount();
			Integer reviewCardCountTarget = studyList.getStudyScheme().getReviewCardStudyScheme().getMaxReviewCount();
			studyList.getStudyScheme().getNewCardStudyScheme().setNewCardCount(newCardCountTarget-newCardStudyCount);
			studyList.getStudyScheme().getReviewCardStudyScheme().setMaxReviewCount(reviewCardCountTarget-reviewCardStudyCount);
			deckBiz
					.getDeck_Note_NoteType_CardStyle_CardCountByStudyList(studyList);
		}
		map.put("listStudys", listStudys);

		return "/studyList.jsp";
	}
	@RequestMapping("/getstudylist")
	@ResponseBody
	public void getstudylist(HttpServletRequest request,HttpServletResponse response){
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		List<StudyList> listStudys = islBiz.selectStudyList(userId);// 模拟两个列表
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		for (StudyList studyList : listStudys) {
			// 设置学习方案
			StudyScheme studyScheme = islBiz.selectStudyListByStudyListId(studyList.getStudyListId()).getStudyScheme();
			studyList.setStudyScheme(studyScheme);
			// 设置筛选条件
			ScreenConditions screenConditions = studyList.getScreenConditions();
//			ScreenDate c = new ScreenDate(null, "before", null, new Date());
//			screenConditions.setCardNextStudyTime(c);
			studyList.setScreenConditions(screenConditions);
			// 当前学习列表已经学习的个数
			Integer newCardStudyCount = csrBiz.getTodayNewCardStudyCount(studyList);
			Integer reviewCardStudyCount = csrBiz.getTodayReviewCardStudyCount(studyList);
			// 当前学习方案的限制个数
			Integer newCardCountTarget = studyList.getStudyScheme().getNewCardStudyScheme().getNewCardCount();
			Integer reviewCardCountTarget = studyList.getStudyScheme().getReviewCardStudyScheme().getMaxReviewCount();
			// 根据已学习的个数,来修改学习列表对象中学习方案的限制
			studyList.getStudyScheme().getNewCardStudyScheme().setNewCardCount(newCardCountTarget-newCardStudyCount);
			studyList.getStudyScheme().getReviewCardStudyScheme().setMaxReviewCount(reviewCardCountTarget-reviewCardStudyCount);
			// 拿到个数
			deckBiz.getDeck_Note_NoteType_CardStyle_CardCountByStudyList(studyList);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", studyList.getStudyListName());
			map.put("newCount", studyList.getNewCard());
			map.put("reviewCount", studyList.getOldCard());
			map.put("id", studyList.getStudyListId());
			maps.add(map);
		}
		AJaxUtil.sendJsonByObj(maps, response);
	}

	// 新建学习列表
	@RequestMapping("/insertsl")
	public String insertStudyList(HttpServletRequest request,StudyList asl, Map<String, Object> map) {
		System.out.println("asl:" + asl);
		// 列表名studyListName，还有筛选条件screenCondition
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		asl.setUserId(userId);
		asl.setStudySchemeId("60b925c6-3af4-11e7-a001-1b41f5094e6c");
		asl.setSortCondition(null);

		asl.getScreenConditions().setUserId(userId);

		asl.setScreenConditions(asl.getScreenConditions());
		Boolean insertFlag = islBiz.insertStudyList(asl);
		map.put("insertFlag", insertFlag);
		return "/study/toinsertsl.action";
	}

	@RequestMapping("/toinsertsl")
	public String toInsertStudyList() {

		return "/insert.jsp";
	}

	@RequestMapping("/updatesl")
	@ResponseBody
	public void updateStudyList(StudyList usl, HttpServletResponse response) {
		System.out.println("usl:" + usl);
		// gson的问题

		// StudyList usl1 = new StudyList();
		// usl1.setStudyListId("9842b938-899e-1035-8add-228b51f6dd6f");
		// usl1.setStudyListName("新的");
		// usl1.setScreenConditions_json("deckName");

		islBiz.updateStudyList(usl);

	}

	// 删除学习列表
	@RequestMapping("/deletesl")
	public String deleteStudyList(String studyListId, Map<String, Object> map) {

		System.out.println("----------------------------------");
		Boolean deleteFlag = islBiz.deleteStudyList(studyListId);
		map.put("deleteFlag", deleteFlag);
		return "/study/getsl.action";
	}

	// 通过学习列表学习卡片，传到cardstudy.jsp 利用gson解析
	@RequestMapping("/studybystudylist")
	@ResponseBody

	//public void studybystudylist(HttpServletResponse response, ScreenConditions screenConditions, StudyList studyList) {
		// String studyListId = "4ac2c5a4-3f06-11e7-aa21-5c90bd05de4c";

	public void studybystudylist(HttpServletRequest request,HttpServletResponse response, StudyList studyList) {
		// String studyListId = "4ac2c5a4-3f06-11e7-aa21-5c90bd05de4c";
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();

		String studySchemeId = "60b925c6-3af4-11e7-a001-1b41f5094e6c";
		Integer studySequence = csrBiz.queryStudySequence(studySchemeId);

		// 先找学习列表
		StudyList sl = islBiz.selectStudyListByStudyListId(studyList.getStudyListId());
		
		ScreenConditions screenConditions = sl.getScreenConditions();
//		ScreenDate c = new ScreenDate(null, "before", null, new Date());
//		screenConditions.setCardNextStudyTime(c);
		sl.setScreenConditions(screenConditions);
		Integer newCardStudyCount = csrBiz.getTodayNewCardStudyCount(sl);
		Integer reviewCardStudyCount = csrBiz.getTodayReviewCardStudyCount(sl);
		// 根据已学习的个数,来修改学习列表对象中学习方案的限制
		Integer newCardCountTarget = sl.getStudyScheme().getNewCardStudyScheme().getNewCardCount();
		Integer reviewCardCountTarget = sl.getStudyScheme().getReviewCardStudyScheme().getMaxReviewCount();
		sl.getStudyScheme().getNewCardStudyScheme().setNewCardCount(newCardCountTarget-newCardStudyCount);
		sl.getStudyScheme().getReviewCardStudyScheme().setMaxReviewCount(reviewCardCountTarget-reviewCardStudyCount);
		
		deckBiz.getDeck_Note_NoteType_CardStyle_CardByStudyList(sl);
		
		// 今天已经学习了的新卡片和复习卡片的数量
		StudyScheme studyScheme = sl.getStudyScheme();
		// 限制个数
		List<DeckNoteNoteTypeCardStyleCard> newCards = sl.getNewCardList();
		List<DeckNoteNoteTypeCardStyleCard> oldCards = sl.getReviewCardList(); // 个数

		// 排序 按下次学习时间排序
		// 学习方案 studybystudyScheme
		// 1.先学习新的 2.先复习 3.随意
		// 新到旧
		List<DeckNoteNoteTypeCardStyleCard> cardsInDeck = new ArrayList<DeckNoteNoteTypeCardStyleCard>();
		if (studySequence == 1) {
			cardsInDeck.addAll(newCards);
			cardsInDeck.addAll(oldCards);
			// 旧到新
		} else if (studySequence == 2) {
			cardsInDeck.addAll(oldCards);
			cardsInDeck.addAll(newCards);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("newCardStudyCount", newCards.size());
		map.put("reviewCardStudyCount", oldCards.size());
		map.put("cards", cardsInDeck);
		
		AJaxUtil.sendJsonByObj(map, response);
		// 传值
	}

	// 跳到学习列表页面
	@RequestMapping("/tostudy")
	public String tostudy(String studyListId, Map<String, Object> map) {
		map.put("studyListId", studyListId);
		return "/cardStudy.jsp";
	}

	@RequestMapping("/getstudylistbyid")
	@ResponseBody
	public void getstudylistbyid(String studyListId, HttpServletResponse response) {
		StudyList studyList = islBiz.selectStudyListByStudyListId(studyListId);
		AJaxUtil.sendJsonByObj(studyList, response);
	}

}
